/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.Deposit_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class Deposit_BLL {
    ConnectUnit connection;
    
    public Deposit_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    /**
     * get list of Deposit form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws SQLException
     */
    public HashSet<Deposit_DTO> getDeposits(String condition, String OrderBy) throws SQLException, Exception{ 
        ResultSet result = this.connection.Select("DEPOSIT", condition, OrderBy);
        HashSet<Deposit_DTO> deposits = new HashSet<Deposit_DTO>();    
        while(result.next()){
            Deposit_DTO deposit = new Deposit_DTO();
            //set deposit from resultset
            deposit.setDepositID(result.getString("DepositID"));
            deposit.setBookID(result.getString("BookID"));
            deposit.setBeneficiaryName(result.getString("BeneficiaryName"));
            deposit.setAmountOfMoney(result.getByte("AmountOfMoney"));
            deposit.setDepositingDate(result.getDate("DepositingDate"));
            //add deposit into list of deposit
            deposits.add(deposit);
        }       
        return deposits;
    }
    /**
     * get list of Deposit form database with Condition
     * @param condition
     * @return
     * @throws SQLException
     * @throws Exception 
     */
    public HashSet<Deposit_DTO> getDeposits(String condition) throws SQLException, Exception{
        return this.getDeposits(condition, null);
    }
    
    /**
     * get list of Deposit form database
     * @return
     * @throws SQLException
     * @throws Exception 
     */
    public HashSet<Deposit_DTO> getDeposits() throws SQLException, Exception{
        return  this.getDeposits(null, null);
    }
    
    /**
     * get Deposit from database by DepositID
     * @param ID
     * @return
     * @throws SQLException
     * @throws Exception 
     */
    public Deposit_DTO getDeposits_byDepositID(String ID) throws SQLException, Exception{
        HashSet<Deposit_DTO> deposit = this.getDeposits("DepositID= '"+ ID+ "'");
        if(deposit.size()>=0)
            return deposit.toArray(new Deposit_DTO[deposit.size()])[0];
        return null;
    }
    
    /**
     * get Deposit by Index
     * @param list
     * @param index
     * @return 
     */
    public Deposit_DTO getDepositByIndex(HashSet<Deposit_DTO> list, int index){
        return (index >= list.size()) ? null : list.toArray(new Deposit_DTO[list.size()])[index];
    }
            
    
    /**
     * Insert a Deposit into database
     * @param info
     * @throws Exception 
     */
    public void Insert(Deposit_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("DepositID", info.getDepositID());
        map.put("DepositingDate", info.getDepositingDate());
        map.put("CustomerName", info.getCustomerName());
        map.put("BeneficiaryName", info.getBeneficiaryName());
        map.put("BookID", info.getBookID());
        map.put("AmountOfMoney", info.getAmountOfMoney());
        
        String[] arr = {"DepositID","DepositingDate", "CustomerName", "BeneficiaryName", "BookID", "AmountOfMoney"}; 
        this.connection.Insert("DEPOSIT", map, arr);
    }
    
    /**
     * Update deposit into database
     * @param info
     * @throws Exception 
     */
    public void Update(Deposit_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("DepositID", info.getDepositID());
        map.put("DepositingDate", info.getDepositingDate());
        map.put("CustomerName", info.getCustomerName());
        map.put("BeneficiaryName", info.getBeneficiaryName());
        map.put("BookID", info.getBookID());
        map.put("AmountOfMoney", info.getAmountOfMoney());
        
        this.connection.Update("DEPOSIT", map, "DepositID="+ info.getDepositID());
    }
    
    /**
     * Delete deposit in database
     * @param info
     * @throws Exception 
     */
    public void Delete(Deposit_DTO info) throws Exception{
        this.connection.Delete("DEPOSIT", "DepositID="+info.getDepositID());
    }
}
